package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.DefectDirectlyReq;
import com.ruibang.glass.produce.entity.DefectDirectly;
import com.ruibang.glass.produce.mapper.DefectDirectlyMapper;
import com.ruibang.glass.produce.service.DefectDirectlyService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 生产-理化-缺陷理化送样 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-03-12
 */
@Slf4j
@Service
public class DefectDirectlyServiceImpl extends ServiceImpl<DefectDirectlyMapper, DefectDirectly> implements DefectDirectlyService {

    @Autowired
    private RedisSingleService redisService;

    @Override
    public IPage<DefectDirectly> pageQuery(DefectDirectlyReq defectDirectlyReq) {
        QueryWrapper<DefectDirectly> qw = new QueryWrapper<>();
        qw.lambda().eq(DefectDirectly::getIsDelete, Constants.ZERO_STR);
        qw.lambda().like(StringUtils.isNotBlank(defectDirectlyReq.getCode()), DefectDirectly::getCode, defectDirectlyReq.getCode());
        qw.lambda().eq(StringUtils.isNotBlank(defectDirectlyReq.getType()), DefectDirectly::getType, defectDirectlyReq.getType());
        if (defectDirectlyReq.getStartTime() != null && defectDirectlyReq.getEndTime() != null) {
            qw.lambda().ge(DefectDirectly::getCreateTime, defectDirectlyReq.getStartTime());
            qw.lambda().le(DefectDirectly::getCreateTime, defectDirectlyReq.getEndTime());
        }
        qw.lambda().orderByDesc(DefectDirectly::getCreateTime);
        return this.page(new Page<>(defectDirectlyReq.getPageNo(), defectDirectlyReq.getPageSize()), qw.lambda());
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(DefectDirectly::getId, id).set(DefectDirectly::getIsDelete, Constants.ONE_STR).update();
    }

    @Override
    public void insert(DefectDirectly defectDirectly) {
        defectDirectly.setIsDelete(Constants.ZERO_STR);
        setUserName(defectDirectly);
        this.save(defectDirectly);
    }

    @Override
    public void export(DefectDirectlyReq defectDirectlyReq, HttpServletResponse response) {
        defectDirectlyReq.setPageNo(-1);
        defectDirectlyReq.setPageSize(-1);
        IPage<DefectDirectly> page = this.pageQuery(defectDirectlyReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<DefectDirectly> defectDirectlyList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), DefectDirectly.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(defectDirectlyList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void edit(DefectDirectly defectDirectly) {
        setUserName(defectDirectly);
        this.updateById(defectDirectly);
    }

    private void setUserName(DefectDirectly defectDirectly) {
        String checkById = defectDirectly.getCheckById();
        String createUserId = defectDirectly.getCreateUserId();
        if (StringUtils.isNotBlank(checkById)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, checkById);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                defectDirectly.setCheckBy(userObj.toString());
            }
        } else {
            defectDirectly.setCheckBy("");
        }
        if (StringUtils.isNotBlank(createUserId)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, createUserId);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                defectDirectly.setCreateUserName(userObj.toString());
            }
        } else {
            defectDirectly.setCreateUserName("");
        }
    }
}
